package com.sand.controller;

import com.alibaba.fastjson.JSON;
import com.sand.entity.InformationEntity;
import com.sand.entity.TRoleEntity;
import com.sand.entity.TUserEntity;
import com.sand.service.IInformationService;
import com.sand.service.IUserService;
import com.sand.util.ModelTest;
import com.sand.util.PageModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping(value = "information")
public class InformationController {
/**
 *
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/12/1/001 20:30
 */

    //userService接口
    @Resource(name = "informationService")
    private IInformationService informationService;

    //分页查询资讯
    @RequestMapping(value = "/queryInformationLimit")
    public void queryInformationLimit(int limit, int page, String condition,int showWi, HttpServletRequest request, HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");

        PageModel<InformationEntity> pm = informationService.queryInformationLimit(limit, page, condition);
        switch (showWi){
            case 1:
                ModelTest<List<InformationEntity>> listData = new ModelTest<List<InformationEntity>>("OK",pm.getList(),0, pm.getPagecount());
                response.getWriter().write(JSON.toJSON(listData).toString());
                break;
            case 2:
                response.getWriter().write(JSON.toJSON(pm.getList()).toString());
                break;
        }
    }
    //删除资讯
    @RequestMapping(value = "/delInformation")
    public void delInformation(String data,HttpServletResponse response)throws Exception{
        boolean bln = informationService.delInformation(data);
        if(bln){
            response.getWriter().write("OK");
        }else{
            response.getWriter().write("NO");
        }
    }
    //添加资讯
    @RequestMapping(value = "/addInformation")
    public void addInformation(String data,HttpServletResponse response)throws Exception{
        boolean bln = informationService.addInformation(data);
        if(bln){
            response.getWriter().write("OK");
        }else{
            response.getWriter().write("NO");
        }
    }
    //根据id查询资讯
    @RequestMapping(value = "/queryInformationById")
    public ModelAndView queryInformationById(int rid,int showWi){
        InformationEntity information = informationService.queryInformationById(rid,showWi);
        ModelAndView mv=new ModelAndView().addObject("upInformation",information);
        switch (showWi){
            case 1://后台数据展示
                mv.setViewName("/layui/page/table_information_show");
            break;
            case 2://前台数据展示
                mv.setViewName("renting/pages/onther/news");
                break;
            case 0://后台修改页面
                mv.setViewName("/layui/page/table/edit_information");
                break;
        }
        return mv;
    }
    //修改资讯
    @RequestMapping(value = "/updateInformation",method = RequestMethod.POST)
    public void updateInformation(String data,HttpServletResponse response)throws Exception{
        boolean bln = informationService.updateInformation(data);
        if(bln){
            response.getWriter().write("OK");
        }else{
            response.getWriter().write("NO");
        }
    }





}
